﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.H2;

namespace LiveResults
{
    [Serializable]
    public class BroadCastProject
    {
        public string EventID { get; set; }
        public string Password { get; set; }
        public string EventName { get; set; }
        public DateTime EventDate { get; set; }

        public EventSoftware EventSoftwareType { get; set; }

        public override string ToString()
        {
            return EventDate.ToString("yyyy-MM-dd") + " - " + EventName;
        }
    }

    [Serializable]
    public abstract class EventSoftware
    {
    }

    [Serializable]
    public class EventSoftwareOE : EventSoftware
    {
        public string Version { get; set; }
        public string Directory { get; set; }
        public string FileExt { get; set; }
    }

    [Serializable]
    public class EventSoftwareOLA : EventSoftware
    {
        public OlaParser.OLA_CONNECTION_TYPE ConnectionType { get; set; }
        public string ConnectionSettings { get; set; }

        public int EventID { get; set; }
        public int RaceID { get; set; }

        public string[] GetDatabases()
        {
            IDbConnection conn = GetDBConnection();
            List<string> dbs = new List<string>();
            if (conn is OleDbConnection)
            {
                OleDbConnection oleConn = conn as OleDbConnection;
                DataTable schemas = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Catalogs, null);
                foreach (DataRow r in schemas.Rows)
                {
                    dbs.Add(r["CATALOG_NAME"] as string);
                }
            }
            else if (conn is MySql.Data.MySqlClient.MySqlConnection)
            {
                IDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SHOW DATABASES";
                IDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string db = reader[0] as string;
                    if (db != "mysql" && db != "information_schema")
                    {
                        dbs.Add(db);
                    }
                }
                reader.Close();
            }
            return dbs.ToArray();
        }

        public IDbConnection GetDBConnection()
        {
            return GetDBConnection(null);
        }

        private IDbConnection GetDBConnection(string schema)
        {
            string[] parts;
            switch (ConnectionType)
            {
                case  OlaParser.OLA_CONNECTION_TYPE.OLA5_INTERNAL_DB:
                    return new H2Connection("jdbc:h2://" + ConnectionSettings.Replace(".h2.db", "") + ";AUTO_SERVER=TRUE", "live", "live");
                case OlaParser.OLA_CONNECTION_TYPE.OLA4_MYSQL:
                case OlaParser.OLA_CONNECTION_TYPE.OLA5_MYSQL:
                    parts = ConnectionSettings.Split(';');
                    return new MySql.Data.MySqlClient.MySqlConnection("Server=" + parts[0] + ";User Id=" + parts[1] + ";Port=" + parts[2] + ";Password=" + parts[3] + (schema != null ? ";Initial Catalog=" + schema : ""));
                case OlaParser.OLA_CONNECTION_TYPE.OLA5_SQLSERVER:
                    parts = ConnectionSettings.Split(';');
                    return new OleDbConnection("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + parts[1] + ";Password=" + parts[3] + ";Data Source=" + parts[0] + (schema != null ? ";Initial Catalog=" + schema : ""));
            }
            return null;
        }
    }
}
